package com.wva.video.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wva.common.utils.Assert;
import com.wva.video.domain.po.VideoMtmTag;
import com.wva.video.domain.po.VideoTag;
import com.wva.video.mapper.VideoTagMapper;
import com.wva.video.service.VideoMtmTagService;
import com.wva.video.service.VideoTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hong
 * @since 2025-02-18
 */
@Service
public class VideoTagServiceImpl extends ServiceImpl<VideoTagMapper, VideoTag> implements VideoTagService {
    @Autowired
    VideoMtmTagService videoMtmTagService;

    @Override
    @Transactional
    public void setVideoTags(Long videoId, String[] tags) {
        Assert.notNull(videoId);
        if (ArrayUtil.isEmpty(tags)) return;

        //1、查询已经存在的标签
        Set<VideoTag> noExistsTags = Arrays.stream(tags).map(VideoTag::new).collect(Collectors.toSet());
        List<VideoTag> filterVideoTags = lambdaQuery().in(VideoTag::getName, Arrays.stream(tags).collect(Collectors.toList())).list();
        for (VideoTag filterVideoTag : filterVideoTags) {
            if (noExistsTags.contains(filterVideoTag)) {
                noExistsTags.remove(filterVideoTag);
            }
        }
        //2、插入不存在的标签
        saveBatch(noExistsTags);

        //3、删除所有当前视频已经绑定的标签
        videoMtmTagService.lambdaUpdate().eq(VideoMtmTag::getVideoId, videoId).remove();

        //4、查出需要绑定的标签的id
        List<VideoTag> waitBindTagIds = lambdaQuery().in(VideoTag::getName, Arrays.stream(tags).collect(Collectors.toList()))
                .list();

        //5、新增视频标签绑定关系
        List<VideoMtmTag> videoMtmTags = waitBindTagIds.stream()
                .map(videoTag ->
                             VideoMtmTag.builder()
                                     .videoId(videoId)
                                     .tagId(videoTag.getId())
                                     .build()
                )
                .collect(Collectors.toList());
        videoMtmTagService.saveBatch(videoMtmTags);
    }

    @Override
    public List<String> listNameByVideoId(Long videoId) {
        List<Long> tagIds = listIdByVideoId(videoId);
        List<VideoTag> videoTags = Collections.emptyList();
        if (CollectionUtil.isNotEmpty(tagIds)) {
            videoTags = listByIds(tagIds);
        }
        return videoTags.stream().map(VideoTag::getName).collect(Collectors.toList());
    }

    public List<Long> listIdByVideoId(Long videoId) {
        List<VideoMtmTag> videoMtmTags = videoMtmTagService.lambdaQuery()
                .eq(VideoMtmTag::getVideoId, videoId)
                .list();

        return videoMtmTags.stream().map(VideoMtmTag::getTagId)
                .collect(Collectors.toList());
    }
}
